package com.dotflat.fin.controller;

import com.dotflat.fin.cnst.Const;
import com.dotflat.fin.domain.FinPlanOrder;
import com.dotflat.fin.service.AliPayService;
import com.dotflat.fin.service.FinPlanOrderService;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.exception.BusinessException;
import com.matt.domain.Clientele;
import com.matt.foundation.model.ErrorCode;
import com.matt.foundation.model.StatusResponse;
import com.matt.service.ClienteleService;
import com.matt.service.ContractService;
import com.matt.remoteCall.SsoCallService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

import static com.matt.foundation.constant.CommonConstant.TICKET;
import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 * Filename: AliAppPay.java <br>
 * <p>
 * Description: 支付宝app支付<br>
 *
 * @author:Ron<br>
 * @version: 1.0 <br>
 * @Createtime: 2020-11-07 <br>
 * <p>
 * Modification History:
 * Date         Author          Version            Description
 * ---------------------------------------------------------*
 * 2019-12-07      Ron           v1.0              第一次创建
 * @Copyright: Copyright (c)2019 byRon<br>
 */
@Slf4j
@RestController
@RequestMapping("/pay/alipay")
@Api(tags = "财务 - 支付宝支付")
public class PayAliController  {

    @Autowired
    private AliPayService payService;

    @Autowired
    private SsoCallService ssoCallService;

    @Autowired
    FinPlanOrderService finPlanOrderService;

    @Autowired
    ContractService contractService;

    @Autowired
    ClienteleService clienteleService;

    @RequestMapping(value = "/prepare",method = RequestMethod.POST)
    public Object getAlipayOrder(@RequestHeader(TICKET) String ticket,
                                 @RequestParam Integer resourceType,
                                 @RequestParam String openId,
                                 @RequestParam(defaultValue = "0") int useDeposit,
                                 @RequestParam String resourceId){

        StaffDTO user = ssoCallService.getStaffDTO(ticket);

        Const.RES_TYPE resType = Const.RES_TYPE.get(resourceType);

        Clientele clientele = null;
        Map result = null;

        if (resType == Const.RES_TYPE.PRODUCT){
            FinPlanOrder order = finPlanOrderService.get(user.getAreaId(), user.getId(), resourceId);

            if (order == null){
                throw new BusinessException(ErrorCode.MATERIAL_NOT_EXIST, "账单不存在");
            }

            clientele = clienteleService.getById(order.getClienteleId());

            // 请求预付单id
            result = payService.prepare(clientele, openId, resourceType, order, useDeposit);
        }

        if (result == null){
            return StatusResponse.error(ErrorCode.SIGN_ERROR, "支付失败");
        }

        return StatusResponse.success(result);
    }


    @RequestMapping(value = "/check", method = GET )
    public Object wxOrderCheck(@RequestHeader(TICKET) String ticket,
                               @RequestParam("resourceType") Integer resourceType,
                               @RequestParam("outTradeNo") String outTradeNo) {

        payService.check(resourceType, outTradeNo);

        return StatusResponse.success();
    }
}
